/**
 * 深拷贝函数
 */
export const deepClone = (data: any) => {
  if (!data || !(data instanceof Object) || typeof data == 'function') {
    return data || undefined
  }
  const constructor = data.constructor
  const result = new constructor()
  for (const key in data) {
    // eslint-disable-next-line no-prototype-builtins
    if (data.hasOwnProperty(key)) {
      result[key] = deepClone(data[key])
    }
  }
  return result
}

/**
 * 对象合并函数
 */
export const mergeObject = function (firstObj: any, secondObj: any) {
  for (const item in secondObj) {
    try {
      if (secondObj[item].constructor === Object) {
        firstObj[item] = mergeObject(firstObj[item], secondObj[item])
      } else if (secondObj[item].constructor === Array) {
        firstObj[item] = mergeObject(firstObj[item], secondObj[item])
      } else {
        firstObj[item] = secondObj[item]
      }
    } catch (e) {
      firstObj[item] = secondObj[item]
    }
  }
  return firstObj
}
